home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15305 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.0 KB

  1. Path: news.austin.sar.slb.com!usenet
  2. From: Chad Houston <chouston@slb.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: [Q] String class problem
  5. Date: Thu, 04 Apr 1996 12:34:54 -0600
  6. Organization: Schlumberger GeoQuest
  7. Message-ID: <3164164E.6E6F@slb.com>
  8. NNTP-Posting-Host: talkingheads.austin.sar.slb.com
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 2.01 (X11; I; SunOS 5.4 sun4m)
  13.  
  14. I've recently hit a compiler snag on the SGI with a string class that we've
  15. been using for a while.  It compiles fine on our Solaris 2.4 compilers, but 
  16. not on IRIX 5.3.  Regardless of the platform, maybe someone can recognize an
  17. error in my code.
  18.  
  19. Any help would be greatly appreciated.
  20.  
  21. Here's a stripped down version of the class:
  22.  
  23.      util_string.hh
  24.      1
  25.      2  class util_String
  26.      3  {
  27.      4  public:
  28.      5      util_String(const char*);
  29.      6      util_String(const util_String&);
  30.      7      util_String(int);
  31.      8      util_String(float);
  32.      9      util_String(double);
  33.     10      util_String();
  34.     11      ~util_String() { delete Str; }
  35.     12
  36.     13      util_String& operator=(const util_String&);
  37.     14      util_String& operator+=(const util_String&);
  38.     15      operator char*();
  39.     16
  40.     17  private:
  41.     18      int Length;
  42.     19      char *Str;
  43.     20  };
  44.  
  45.  
  46. Here is a short test program which illustrates the compiler problem:
  47.  
  48.      string_test.cc
  49.      1
  50.      2  #include <util_string.hh>
  51.      3
  52.      4  int
  53.      5  main()
  54.      6  {
  55.      7      int        iVal = 5;
  56.      8      float      fVal = 5.12345;
  57.      9      char       cpSpace[] = " ";
  58.     10      char       cpVal[] = "Test String";
  59.     11      util_String MyString;
  60.     12
  61.     13      MyString = cpVal;
  62.     14      MyString += cpSpace;
  63.     15      MyString += iVal;
  64.     16      // MyString += (util_String) iVal;
  65.     17      // MyString.cps_String::operator+=(iVal);
  66.     18      MyString += cpSpace;
  67.     19      MyString += fVal;
  68.     20  }
  69.  
  70.  
  71. The SGI /bin/CC compiler returns the following error:
  72.  
  73. test(200)% !CC
  74. CC -c -I. string_test.cc
  75. "string_test.cc", line 18: error(3431): more than one operator "+=" matches
  76.           these operands:
  77.             built-in operator "pointer += integer"
  78.             function "cps_String::operator+="
  79.       MyString += iVal;
  80.  
  81.  
  82. Apparently, it can't determine that it should use the util_String(int) constructor,
  83. and then call the util_String::operator+=(const util_String&) method.  However, the
  84. file *will* compile if I comment out line 15 in the header file.  That is, if
  85. I remove my method to cast a util_String to a char *.  Is there something wrong
  86. with the syntax of my cast operator, operator char *() ?
  87.  
  88. Note that the file will also compile if I comment out line 15 in string_test.cc 
  89. and use either line 16 or 17.
  90.  
  91. -- 
  92. +--------------+    
  93. |______________|    Chad Houston                       <chouston@slb.com>
  94. | Schlumberger |    Senior Software Engineer            CPS-3 Engineering
  95. +--------------+    Austin, TX, USA
  96.     GeoQuest            Phone: 512.331.3384  Fax: 512.331.3387
  97.